const Nodes = require("../model/nodes.model.js");

class NodesService {
  //保存公司信息，如果没有companyId则为新增，否则为更新
  async saveNodes({
    nodeId,
    nodeName,
    nodeCode,
    nodeType,
    wavelength,
    distance,
    joinTime,
    nodeAttr,
    station,
    stationPort,
    extendChannel,
    stationName,
    latitude,
    longitude
  }) {
    var update = false;
    var nodes = {};
    var whereOpt = {};
    if (nodeId && nodeId > 0) {
      update = true;
      whereOpt.nodeId = nodeId;
    }
    nodeName && Object.assign(nodes, { nodeName });
    nodeCode && Object.assign(nodes, { nodeCode });
    nodeType && Object.assign(nodes, { nodeType });
    wavelength > 0 && Object.assign(nodes, { wavelength });
    distance > 0 && Object.assign(nodes, { distance });
    nodeAttr && Object.assign(nodes, { nodeAttr });
    station && Object.assign(nodes, { station });
    stationPort && Object.assign(nodes, { stationPort });
    extendChannel && Object.assign(nodes, { extendChannel });
    stationName && Object.assign(nodes, { stationName });
    longitude && Object.assign(nodes, { longitude });
    latitude && Object.assign(nodes, { latitude });
    if (!update) {
      joinTime = new Date().getTime();
    }
    joinTime > 0 && Object.assign(nodes, { joinTime });
    if (Object.keys(nodes).length == 0) {
      return false;
    }
    var res;
    try {
      if (update) {
        res = await Nodes.update(nodes, { where: whereOpt });
        if (res[0] > 0) {
          nodes.nodeId = nodeId;
          return nodes;
        } else {
          return false;
        }
      } else {
        res = await Nodes.create(nodes);
        if (res.dataValues == undefined) {
          return false;
        } else {
          return res.dataValues;
        }
      }
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  //删除nodes
  async eraseNodeById(nodeId) {
    if (!nodeId || nodeId <= 0) {
      return false;
    }
    var res;
    try {
      res = Nodes.destroy({ where: { nodeId } });
      if (!res) {
        return false;
      }
      return true;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
    /* 
  删除指定条件的节点
  */
 async eraseNodes({linkName,nodeType}){
  var whereOpt = {};
  linkName && Object.assign(whereOpt,{linkName});
  nodeType && Object.assign(whereOpt,{nodeType});
  if(Object.keys(whereOpt).length == 0){
    return false;
  }
  var res;
  try {
    res = await Nodes.destroy({where:whereOpt});
    if(!res){
      return false;
    }else{
      return true;
    }
  } catch (error) {
    console.log(error);
    return null;
  }
 }
   /* 
  批量添加
  */
  async  batchAddNodes(arr){
    if(!(arr instanceof Array) || arr.length == 0){
      return false;
    }
    var res;
    try {
      res = await Nodes.bulkCreate(arr,{
        fields:[
          "nodeName",
          "nodeCode",
          "nodeType",
          "codeInfo",
          "wavelength",
          "distance",
          "joinTime",
          "nodeAttr",
          "station",
          "stationPort",
          "extendChannel",
          "linkName",
          "longitude",
          "latitude"
        ]
      });
      return res;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  /*
   *查找指定条件的公司
   *@page:分页选项
   *@page.pageIndex:当前第几页
   *@page.pageSize:每页多少条记录
   *@page.pageCount:总共多少页
   *@page.sum:总共多少条记录
   */
  async getNodes({ nodeId, nodeName, nodeType, linkName }) {
    var whereOpt = {};
    nodeId >= 0 && Object.assign(whereOpt, { nodeId });
    nodeName && Object.assign(whereOpt, { nodeName });
    nodeType && Object.assign(whereOpt, { nodeType });
    linkName && Object.assign(whereOpt, { linkName });
    var res;
    try {
      res = await Nodes.findAll({
        where: whereOpt,
        order: [["nodeId", "ASC"]],
      });
      if(!res || res == null){
        return [];
      }
      return JSON.parse(JSON.stringify(res));
    } catch (error) {
      console.log(error);
      return null;
    }
  }
}

module.exports = new NodesService();
